/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.GroupsDAO;
import com.ivanbiz.dao.impl.GroupsDAOImpl;
import com.ivanbiz.model.Groups;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class GroupService extends SelectorComposer<Component> {

    GroupsDAO groupsDAO;
    @Wire
    Listbox listGroup;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        groupsDAO = new GroupsDAOImpl();
        loadGroup();
    }

    private void loadGroup() {
        try {
            listGroup.getChildren().clear();
            int no = 0;
            int x = 0;
            List<Groups> loadGroup = groupsDAO.getAll(Groups.class);
            for (Groups group : loadGroup) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(group);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(group.getNama()));
                listGroup.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(GroupService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#tambah;")
    public void tambah() {
        Window win = (Window) Executions.createComponents("/groupAction.zul", null, null);
        win.doModal();
        loadGroup();
    }

    @Listen("onClick=#ubah;")
    public void ubah() {
        if (listGroup.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di ubah");
        } else {
            Groups groups = listGroup.getSelectedItem().getValue();
            Map map = new HashMap();
            map.put("group", groups);
            Window win = (Window) Executions.createComponents("/groupAction.zul", null, map);
            win.setTitle("Ubah Group");
            win.doModal();
            loadGroup();
        }
    }

    @Listen("onClick=#hapus;")
    public void hapus() throws Exception {
        if (listGroup.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di hapus");
        } else {
            Groups groups = listGroup.getSelectedItem().getValue();
            groupsDAO.delete(groups);
            loadGroup();
        }
    }
}
